package com.vitas.coin.ui.adapter

import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.RecyclerView
import com.vitas.coin.R
import com.vitas.coin.audio.AudioData
import com.vitas.coin.data.bean.ChildrenVoiceBean
import com.vitas.coin.databinding.ItemMusicBinding
import com.vitas.coin.manager.LocalMusicManager
import com.vitas.coin.ui.act.PlayAct
import com.vitas.coin.util.Audio
import com.vitas.coin.util.Utils
import java.io.File

class LocalAdapter(var context: FragmentActivity, var list: MutableList<AudioData>?) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    var inflater: LayoutInflater = LayoutInflater.from(context)

    fun resetList(list: MutableList<AudioData>?) {
        this.list = list
        notifyDataSetChanged()
    }

    fun appendList(list: MutableList<AudioData>) {
        val start = this.list!!.size
        this.list!!.addAll(list)
        notifyItemRangeInserted(start, list.size)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val binding = ItemMusicBinding.inflate(inflater, parent, false)
        return Holder(binding)
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is Holder) {
            holder.bind(position)
        }
    }

    override fun getItemCount(): Int {
        return if (list == null) 0 else list!!.size
    }

    inner class Holder(var binding: ItemMusicBinding) : RecyclerView.ViewHolder(
        binding.root
    ) {
        fun bind(p: Int) {
            val item = list!![p]
            binding.ivCover.setImageResource(R.drawable.ic_control_logo)
            binding.tvName.text = item.name
            binding.tvDuration.text =
                LocalMusicManager.getInstance(context).duration(item.duration / 1000)
            if (item.isFavorite) {
                binding.ivStar.setImageResource(R.mipmap.icon_star_collected)
            } else {
                binding.ivStar.setImageResource(R.mipmap.icon_star_normal)
            }
            binding.ivLingsheng.setOnClickListener {
                Utils.setSound(context, File(item.path), item.name)
            }
            binding.root.setOnClickListener {
                var i = Intent(binding.root.context, PlayAct::class.java)
                i.putExtra("index", adapterPosition)
                i.putParcelableArrayListExtra("list", getList())
                binding.root.context.startActivity(i)
//                var bean = MusicBean(item.name, "file://" + item.path)
//                PlayManager.getInstance().onSwitchMusicPlay(bean)
            }
            binding.cut.setOnClickListener {
                if (Utils.checkRight(context)) {
                    Audio.openEdit(context, item.path, item.name)
                }
            }
        }
    }

    fun getList(): ArrayList<ChildrenVoiceBean> {
        var tmp = ArrayList<ChildrenVoiceBean>()
        list?.let {
            for (item in it) {
                var bean =
                    ChildrenVoiceBean(
                        item.addTime.toInt(),
                        item.name,
                        ("file://" + item.path),
                        item.isFavorite
                    )
                tmp.add(bean)
            }
        }
        return tmp
    }
}
